<?php
defined('APP') or die('Hacking attemp!');

class Model_Quanlysach extends Core_Model
{
	public function __construct(){
		parent::__construct();
	}
	public function getAllSach(){
		$query = 'SELECT * FROM book ORDER BY BookID DESC';
		$result = Core_Model::excuteQuery($query);
		return $result;
		Core_Model::freeResult($result);
	}
	public function getdmSach(){
		$query = 'SELECT * FROM category';
		$result = Core_Model::excuteQuery($query);
		return $result;
		Core_Model::freeResult($result);
	}
	

	public function getNhaxuatbanSach(){
		$query = 'SELECT * FROM publisher';
		$result = Core_Model::excuteQuery($query);
		return $result;
		Core_Model::freeResult($result);
	}

	public function getdmSachID($cat){
		if(!empty($cat)){
			$query = 'SELECT * FROM category INNER JOIN book ON category.CategoryID = book.BookCatID AND category.CategoryID = "'.$cat.'" ORDER BY BookID DESC';
			$result = Core_Model::excuteQuery($query);
			return $result;
			Core_Model::freeResult($result);
		}
	}
	
	public function getSachID($idsach){
		if(!empty($idsach)){
			$query = 'SELECT * FROM category INNER JOIN book ON category.CategoryID = book.BookCatID INNER JOIN publisher ON publisher.PublisherID = book.BookPubID AND book.BookID = "'.$idsach.'" ORDER BY BookID DESC';
			$result = Core_Model::excuteQuery($query);
			return $result;
			Core_Model::freeResult($result);
		}
	}

    public function getTimkiem($timkiem){
        if(!empty($timkiem)){
            $query = 'SELECT * FROM book WHERE BookTitle LIKE "%'.$timkiem.'%" ORDER BY BookID DESC';
            $result = Core_Model::excuteQuery($query);
            return $result;
            Core_Model::freeResult($result);
        }
    }

	public function getTotalDanhmucID($id){
		if(!empty($id)){
			$query = 'SELECT BookID FROM category INNER JOIN book ON category.CategoryID = book.BookCatID AND book.BookCatID = "'.$id.'"';
			$result = Core_Model::excuteQuery($query);
            return $result->num_rows;
            Core_Model::freeResult($result);
		}
	}


	public function getTotalPublichserID($id){
		if(!empty($id)){
			$query = 'SELECT BookID FROM publisher INNER JOIN book ON publisher.PublisherID = book.BookPubID AND book.BookPubID = "'.$id.'"';
			$result = Core_Model::excuteQuery($query);
            return $result->num_rows;
            Core_Model::freeResult($result);
		}
	}


	//Admin Thêm mới danh mục
	public function getInsertDanhmuc($catname){
		if(!empty($catname)){
			$query = 'INSERT INTO category(CategoryName) VALUES("'.$catname.'")';
			$result = Core_Model::excuteQuery($query);
			if($result){
				return true;
			}else{
				return false;
			}
			Core_Model::freeResult($result);
		}
		return false;
	}

	//Admin Thêm mới nhà xuất bản
	public function getInsertNhaxuatban($pubname){
		if(!empty($pubname)){
			$query = 'INSERT INTO publisher(PublisherName) VALUES("'.$pubname.'")';
			$result = Core_Model::excuteQuery($query);
			if($result){
				return true;
			}else{
				return false;
			}
			Core_Model::freeResult($result);
		}
		return false;
	}

	//Admin Thêm mới sách
	public function getInsertSach($BookCatID, $BookPubID, $BookTitle, $BookImg ,$BookPrice ,$BookAuthor ,$BookYear, $BookDesc){
		if(!empty($BookCatID) || !empty($BookPubID) || !empty($BookTitle) || !empty($BookImg) || !empty($BookPrice) || !empty($BookAuthor) || !empty($BookYear) || !empty($BookDesc)){
			$query = 'INSERT INTO book(BookCatID, BookPubID, BookTitle, BookImg, BookPrice, BookAuthor, BookYear, BookDesc) VALUES("'.$BookCatID.'","'.$BookPubID.'","'.$BookTitle.'","'.$BookImg.'","'.$BookPrice.'","'.$BookAuthor.'","'.$BookYear.'","'.$BookDesc.'")';
			$result = Core_Model::excuteQuery($query);
			if($result){
				return true;
			}else{
				return false;
			}
			Core_Model::freeResult($result);
		}
		return false;
	}

	//Admin xóa danh mục

	public function xoadanhmuc($id){
		if(!empty($id)){
			
			return true;
		}
		return false;
	}
}